﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace dzs.Web.tenderExpert
{
    public partial class AddOrUpExpertInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int expertInfoId = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(expertInfoId);
                }
            }
        }

        protected void btn_confirm_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtexpertName.Text.Trim().Length == 0)
            {
                strErr += "姓名不能为空！\\n";
            }
            //if (this.txtunit.Text.Trim().Length == 0)
            //{
            //    strErr += "单位不能为空！\\n";
            //}
            //if (this.txtcontactway.Text.Trim().Length == 0)
            //{
            //    strErr += "联系方式不能为空！\\n";
            //}

            if (cblexpertMajor.SelectedIndex < 0)
            {
                strErr += "专业选择不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string expertName = this.txtexpertName.Text;
            string unit = this.txtunit.Text;
            string contactway = this.txtcontactway.Text;
            string expertStudyMajor = txtexpertStudyMajor.Text;
            string expertMajor = "";
            Maticsoft.Model.expertInfo model = new Maticsoft.Model.expertInfo();

            for (int i = 0; i < cblexpertMajor.Items.Count; i++)
            {
                if (cblexpertMajor.Items[i].Selected)
                {
                    string mvalue = cblexpertMajor.Items[i].Value;

                    expertMajor += mvalue + ",";

                    if (mvalue == "商务综合")
                        model.商务综合 = true;
                   
                    if (mvalue == "变电一次")
                        model.变电一次 = true;
                  
                    if (mvalue == "变电二次")
                        model.变电二次 = true;
                 
                    if (mvalue == "输电线路")
                        model.输电线路 = true;
                  
                    if (mvalue == "10KV配电")
                        model.s10KV配电 = true;
                  
                    if (mvalue == "安全工具")
                        model.安全工具 = true;
                   
                    if (mvalue == "原材料")
                        model.原材料 = true;
                   
                    if (mvalue == "勘察")
                        model.勘察 = true;
                    
                    if (mvalue == "设计")
                        model.设计 = true;
                   
                    if (mvalue == "施工")
                        model.施工 = true;
                    
                    if (mvalue == "监理")
                        model.监理 = true;

                    if (mvalue == "配电")
                        model.配电 = true;

                    if (mvalue == "基建")
                        model.基建 = true;

                    if (mvalue == "服务类")
                        model.服务类 = true;
                    
                }
               
            }

           
            model.expertName = expertName;
            model.unit = unit;
            model.contactway = contactway;
            model.expertStudyMajor = expertStudyMajor;
          
            if (expertMajor != "")
            {
                expertMajor = expertMajor.Substring(0, expertMajor.LastIndexOf(","));
            }
            model.expertMajor = expertMajor;

            if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
            {
                model.expertInfoId = int.Parse(this.lblexpertInfoId.Text);

                Maticsoft.BLL.expertInfo bll = new Maticsoft.BLL.expertInfo();
                bll.Update(model);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListExpertInfo.aspx");
            }
            else
            {
                Maticsoft.BLL.expertInfo bll = new Maticsoft.BLL.expertInfo();
                bll.Add(model);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListExpertInfo.aspx");
            }

        }

        private void ShowInfo(int expertInfoId)
        {
            Maticsoft.BLL.expertInfo bll = new Maticsoft.BLL.expertInfo();
            Maticsoft.Model.expertInfo model = bll.GetModel(expertInfoId);
            this.lblexpertInfoId.Text = model.expertInfoId.ToString();
            this.txtexpertName.Text = model.expertName;
            this.txtunit.Text = model.unit;
            this.txtcontactway.Text = model.contactway;
            this.txtexpertStudyMajor.Text = model.expertStudyMajor;

            if (model.expertMajor != "")
            {
                string[] strs = model.expertMajor.Split(',');

                if (strs.Length > 0)
                {
                    foreach (string str in strs)
                    {
                        for (int i = 0; i < this.cblexpertMajor.Items.Count; i++)
                        {
                            if (cblexpertMajor.Items[i].Value == str)
                            {
                                cblexpertMajor.Items[i].Selected = true;
                            }
                        }
                    }
                }
            }

        }
    }
}